package fr.enseirbmatmeca.apms.spring.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import fr.enseirbmatmeca.apms.hibernate.dao.UserDAO;
import fr.enseirbmatmeca.apms.spring.model.project.Project;
import fr.enseirbmatmeca.apms.spring.model.user.User;
import fr.enseirbmatmeca.apms.spring.security.ProjectRole;
import fr.enseirbmatmeca.apms.spring.service.UserManager;

/**
 * Implements business features to handler users
 * @author bmeurant, coudar
 */
@Service("userManager")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class UserManagerImpl implements UserManager {

    private final Log logger = LogFactory.getLog(UserManagerImpl.class);
    
    @Autowired
    private UserDAO userDAO = null;

    /**
     * {@inheritDoc}
     */
    public String changePassword(String login, String password) {
        User user = userDAO.getUser(login);
        if (user != null) {
        	return userDAO.updateUser(user);
        }
        return "User not found.";
    }
    
    /**
     * {@inheritDoc}
     */
    public boolean checkLogin (String login, String password) {
        return userDAO.checkLogin(login, password);
    }

    @Transactional(readOnly = false)
	public String createUser(User user) {
		return this.userDAO.createUser(user);
	}
    
    public User createUser(String login, String pushToken) {
		System.out.print("UserManagerImpl : createUser : " + login);
		User user = userDAO.createUser(login, pushToken);
		return user;
	}
    
    @Transactional(readOnly = false)
	public String delete(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<User> findAll() {
		return userDAO.findAll();
	}

	/**
     * {@inheritDoc}
     */
    public User getUser(String login) {
        return userDAO.getUser(login);
    }

	/**
     * setter to allows spring to inject userDAO implementation
     * @param userDAO : object (implementation of userDAO interface) to inject.
     */
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

	@Transactional(readOnly = false)
	public String update(User user) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<ProjectRole> getProjectRoles(User user, Project project){
		return userDAO.getProjectRoles(user, project);
	}

	public boolean saveUser(User user) {
		return userDAO.saveUser(user);
	}
}